package com.lyn.module_custom_view.progress;


import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;

/**
 * @author:longyongning
 * @date:2021/10/2
 * @description:进度显示控件
 */
public class ProgressLineView extends View {

    private Paint paint;

    private float schedule;

    private int currentProgress;

    private int progress;

    private int width;

    private int height;

    private int frameColor=Color.GREEN;

    private int progressBarColor=Color.RED;


    public void setFrameColor(int frameColor) {
        this.frameColor = frameColor;
        postInvalidate();
    }

    public void setProgressBarColor(int progressBarColor) {
        this.progressBarColor = progressBarColor;
        postInvalidate();
    }

    public ProgressLineView(Context context) {
        super(context);
        init();
    }

    public ProgressLineView(Context context,  AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    private void init() {
        paint = new Paint();
        paint.setAntiAlias(true);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        width = canvas.getWidth();
        height = canvas.getHeight();
        paint.setStyle(Paint.Style.STROKE);
        paint.setColor(frameColor);
        canvas.drawRoundRect(new RectF(0, 0, width, height), 15, 15, paint);
        paint.setColor(progressBarColor);
        paint.setStyle(Paint.Style.FILL);
        float padding=3;
        canvas.drawRoundRect(new RectF(0+padding, 0+padding, width / 100 * currentProgress-padding, height-padding), 15, 15, paint);

    }

    public void setProgress(int progress) {
        this.progress = progress;
        ValueAnimator valueAnimator = ValueAnimator.ofInt(currentProgress, progress);
        valueAnimator.setDuration(300);
        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator valueAnimator) {
                currentProgress = (int) valueAnimator.getAnimatedValue();
                invalidate();
            }
        });
        valueAnimator.start();

    }
}
